package com.bdk.middle.management.web.tenant;


import com.bdk.middle.common.domain.PageDO;
import com.bdk.middle.common.result.ResultDO;
import com.bdk.middle.common.result.web.ObjectResponse;
import com.bdk.middle.tenant.api.client.TenantClientApi;
import com.bdk.middle.tenant.api.domain.TenantDO;
import com.bdk.middle.tenant.api.domain.TenantReviewCommentDO;
import com.bdk.middle.tenant.api.query.TenantQueryDO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


/**
 * <p>
 * 用户登录表 前端控制器
 * </p>
 *
 * @author Reagan
 * @since 2021-01-23
 */
@Api(tags = "租户信息管理相关接口")
@RestController
@RequestMapping("/tenant")
public class TenantController {

    @Autowired
    private TenantClientImpl tenantClientImpl;

    @FeignClient(value= "tenant-service", contextId="tenant", path = "/app")
    interface TenantClientImpl extends TenantClientApi {

    }

    @ApiOperation(value="租户新增", notes="租户新增")
    @RequestMapping(value="/add.html", method = RequestMethod.POST)
    public @ResponseBody ObjectResponse add(@RequestBody TenantDO domain) {

        ObjectResponse response = new ObjectResponse();

        ResultDO result = tenantClientImpl.add(domain);

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }
    @ApiOperation(value="租户编辑", notes="根据json的id来指定编辑对象")
    @RequestMapping(value="/edit.html", method = RequestMethod.POST)
    public @ResponseBody ObjectResponse edit(@RequestBody TenantDO domain) {

        ObjectResponse response = new ObjectResponse();

        ResultDO result = tenantClientImpl.edit(domain);

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }
    @ApiOperation(value="租户删除", notes="根据json的id来指定删除对象")
    @RequestMapping(value="/delete.html", method = RequestMethod.POST)
    public @ResponseBody ObjectResponse delete(@RequestBody TenantDO domain) {

        ObjectResponse response = new ObjectResponse();

        ResultDO result = tenantClientImpl.remove(domain.getId(), null);

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }
    @ApiOperation(value="租户详情", notes="根据json的id来指定对象")
    @RequestMapping(value="/detail.html", method = RequestMethod.POST)
    public @ResponseBody ObjectResponse detail(@RequestBody TenantDO domain) {

        ObjectResponse<Map<Object, Object>> response = new ObjectResponse();

        ResultDO result = tenantClientImpl.get(domain.getId());
        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        } else {
            response.setData((Map<Object, Object>) result.getModel(ResultDO.FIRST_MODEL_KEY));
        }
        return response;
    }
    @ApiOperation(value="租户列表", notes="获取租户列表")
    @RequestMapping(value="/list.html", method = RequestMethod.POST)
    public @ResponseBody ObjectResponse list(@RequestBody TenantQueryDO domain) {

        ObjectResponse<Map<Object, Object>> response = new ObjectResponse();

        ResultDO result = tenantClientImpl.find(domain);

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        } else {
            response.setData((Map<Object, Object>) result.getModel(ResultDO.FIRST_MODEL_KEY));
        }
        return response;
    }
    @ApiOperation(value="租户状态", notes="修改租户状态以及添加拒绝审核记录")
    @RequestMapping(value="/check.html", method = RequestMethod.POST)
    public @ResponseBody ObjectResponse check(@RequestBody TenantReviewCommentDO domain) {

        ObjectResponse<Map<Object, Object>> response = new ObjectResponse();

        ResultDO result = tenantClientImpl.check(domain);
        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        } else {
            response.setData((Map<Object, Object>) result.getModel(ResultDO.FIRST_MODEL_KEY));
        }
        return response;
    }
}

